<template>
  <div class="app-container">
    <!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
    <el-form ref="queryForm" :model="queryParams" label-width="89px" v-show="showSearch" class="ele-form-search"
             @keyup.enter.native="reload"
             @submit.native.prevent>
      <el-row :gutter="15">
        <!--        <el-col :lg="8" :md="12">-->
        <!--    <el-form-item label="产品编码" prop="materialCode">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.materialCode"-->
        <!--          placeholder="请输入产品编码"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :lg="8" :md="12">-->
        <!--          <el-form-item label="产品名称" prop="materialName">-->
        <!--            <el-input-->
        <!--              v-model="queryParams.materialName"-->
        <!--              placeholder="请输入产品名称"-->
        <!--              clearable-->
        <!--              @keyup.enter.native="handleQuery"-->
        <!--            />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :lg="8" :md="12">-->
        <!--      <el-form-item label="库存数量" prop="nventoryQuantity">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.nventoryQuantity"-->
        <!--          placeholder="请输入库存数量"-->
        <!--          clearable-->
        <!--          @input="a"-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :lg="8" :md="12">-->
        <!--&lt;!&ndash;      <el-form-item label="安全库存" prop="safetyStock">&ndash;&gt;-->
        <!--&lt;!&ndash;        <el-input&ndash;&gt;-->
        <!--&lt;!&ndash;          v-model="queryParams.safetyStock"&ndash;&gt;-->
        <!--&lt;!&ndash;          placeholder="请输入安全库存"&ndash;&gt;-->
        <!--&lt;!&ndash;          clearable&ndash;&gt;-->
        <!--&lt;!&ndash;          @input="b"&ndash;&gt;-->
        <!--&lt;!&ndash;          @keyup.enter.native="handleQuery"&ndash;&gt;-->
        <!--&lt;!&ndash;        />&ndash;&gt;-->
        <!--&lt;!&ndash;      </el-form-item>&ndash;&gt;-->
        <!--        </el-col>-->
        <!--        <el-col :lg="8" :md="12">-->
        <!--      <el-form-item label="最高库存" prop="maximumInventory">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.maximumInventory"-->
        <!--          placeholder="请输入最高库存"-->
        <!--          clearable-->
        <!--          @input="c"-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :lg="8" :md="12">-->
        <!--      <el-form-item label="最低库存" prop="minimumStock">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.minimumStock"-->
        <!--          placeholder="请输入最低库存"-->
        <!--          clearable-->
        <!--          @input="d"-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :lg="8" :md="12">-->
        <!--          <el-form-item>-->
        <!--            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
        <!--            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :lg="8" :md="12">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['wareManagement:tnventoryWarning:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['wareManagement:tnventoryWarning:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['wareManagement:tnventoryWarning:remove']"
            >删除
            </el-button>
          </el-col>
        </el-col>
      </el-row>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--    -->
    <!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
    <!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--      &lt;!&ndash;          type="warning"&ndash;&gt;-->
    <!--      &lt;!&ndash;          plain&ndash;&gt;-->
    <!--      &lt;!&ndash;          icon="el-icon-download"&ndash;&gt;-->
    <!--      &lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--      &lt;!&ndash;          @click="handleExport"&ndash;&gt;-->
    <!--      &lt;!&ndash;          v-hasPermi="['wareManagement:tnventoryWarning:export']"&ndash;&gt;-->
    <!--      &lt;!&ndash;        >导出</el-button>&ndash;&gt;-->
    <!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getwarnList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table
      v-loading="loading"
      :data="tnventoryWarningList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品名称" align="center" prop="materialName" width="200"
                       show-overflow-tooltip></el-table-column>
      <el-table-column label="产品编码" align="center" prop="materialCode"></el-table-column>
      <el-table-column label="最高库存" align="center" prop="maximumInventory"></el-table-column>
      <el-table-column label="最低库存" align="center" prop="minimumStock"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['wareManagement:tnventoryWarning:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['wareManagement:tnventoryWarning:remove']"
            >删除</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <!--    <el-table-->
    <!--      v-loading="loading"-->
    <!--      :data="tnventoryWarningList"-->
    <!--      @selection-change="handleSelectionChange"-->
    <!--      :row-class-name="tableRowClassName"-->
    <!--    >-->
    <!--      <el-table-column type="selection" width="55" align="center" />-->
    <!--      <el-table-column label="产品编码" align="center" prop="materialCode">-->
    <!--        <template slot-scope="scope">-->
    <!--          <el-tooltip :content="getTooltipContent(scope.row)" placement="top">-->
    <!--            <span>{{ scope.row.materialCode }}</span>-->
    <!--          </el-tooltip>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="产品名称" align="center" prop="materialName">-->
    <!--        <template slot-scope="scope">-->
    <!--          <el-tooltip :content="getTooltipContent(scope.row)" placement="top">-->
    <!--            <span>{{ scope.row.materialName }}</span>-->
    <!--          </el-tooltip>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="最高库存" align="center" prop="maximumInventory">-->
    <!--        <template slot-scope="scope">-->
    <!--          <el-tooltip :content="getTooltipContent(scope.row)" placement="top">-->
    <!--            <span>{{ scope.row.maximumInventory }}</span>-->
    <!--          </el-tooltip>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="最低库存" align="center" prop="minimumStock">-->
    <!--        <template slot-scope="scope">-->
    <!--          <el-tooltip :content="getTooltipContent(scope.row)" placement="top">-->
    <!--            <span>{{ scope.row.minimumStock }}</span>-->
    <!--          </el-tooltip>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
    <!--        <template slot-scope="scope">-->
    <!--          <el-tooltip :content="getTooltipContent(scope.row)" placement="top">-->
    <!--          <span>-->
    <!--            <el-button-->
    <!--              size="mini"-->
    <!--              type="text"-->
    <!--              icon="el-icon-edit"-->
    <!--              @click="handleUpdate(scope.row)"-->
    <!--              v-hasPermi="['wareManagement:tnventoryWarning:edit']"-->
    <!--            >修改</el-button>-->
    <!--            <el-button-->
    <!--              size="mini"-->
    <!--              type="text"-->
    <!--              icon="el-icon-delete"-->
    <!--              @click="handleDelete(scope.row)"-->
    <!--              v-hasPermi="['wareManagement:tnventoryWarning:remove']"-->
    <!--            >删除</el-button>-->
    <!--          </span>-->
    <!--          </el-tooltip>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--    </el-table>-->

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getwarnList"
    />

    <!-- 添加或修改库存预警查询对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="materialName">
          <el-select clearable v-model="form.materialName" placeholder="请选择产品名称" class="ele-fluid"
                     @change="handleMaterialChange">
            <el-option v-for="item in inventoryInquiryList" :key="item.materialId" :label="item.materialName"
                       :value="item.materialId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="产品编码" prop="materialCode">
          {{ form.materialCode || "--" }}
          <!--          <el-input v-model="form.materialCode" placeholder="请输入产品编码" />-->
        </el-form-item>

        <el-form-item label="库存数量" prop="nventoryQuantity">
          {{ form.nventoryQuantity || "--" }}
          <!--          <el-input @input="aa" v-model="form.nventoryQuantity" placeholder="请输入库存数量" />-->
        </el-form-item>
        <!--        <el-form-item label="安全库存" prop="safetyStock">-->
        <!--          <el-input  @input="bb"  v-model="form.safetyStock" placeholder="请输入安全库存" />-->
        <!--        </el-form-item>-->
        <el-form-item label="最高库存" prop="maximumInventory">
          <el-input @input="cc" v-model="form.maximumInventory" placeholder="请输入最高库存"/>
        </el-form-item>
        <el-form-item label="最低库存" prop="minimumStock">
          <el-input @input="dd" v-model="form.minimumStock" placeholder="请输入最低库存"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaterialManagement
} from "@/api/basicData/materialManagement";
import {
  listInventoryInquiry,
} from "@/api/wareManagement/inventoryInquiry";
import {
  addTnventoryWarning,
  listTnventoryWarning,
  getTnventoryWarning,
  updateTnventoryWarning,
  delTnventoryWarning
} from "../../../api/wareManagement/tnventoryWarning";

export default {
  name: "TnventoryWarning",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      inventoryInquiryList: [],
      // 库存预警查询表格数据
      tnventoryWarningList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        materialCode: null,
        materialName: null,
        nventoryQuantity: null,
        safetyStock: null,
        maximumInventory: null,
        minimumStock: null,
      },
      // 表单参数
      form: {},
      idD: [],
      // 表单校验
      rules: {
        materialCode: [
          {required: true, message: "产品编码不能为空", trigger: "blur"}
        ],
        maximumInventory: [
          {required: true, message: "最高库存不能为空", trigger: "blur"}
        ],
        minimumStock: [
          {required: true, message: "最低库存不能为空", trigger: "blur"}
        ],
        materialName: [
          {required: true, message: "请选择产品名称", trigger: "change"}
        ],
      }
    };
  },
  mounted() {

    this.getwuliaoList();
    this.getwarnList()
  },
  methods: {
    /** 查询产品管理列表 */
    getwuliaoList() {
      listMaterialManagement().then(response => {
        const materialManagementList = response.rows;
        //console.log("666666", materialManagementList)
        this.idD = materialManagementList.map(item => item.materialId);
        this.idD = this.idD.join("，")
        //console.log("idsss", this.idD); // 使用 this.idD
        if (this.idD.length) {
          this.getList();
        }
      });
    },
    getList() {
      this.loading = true;
      this.queryParams.materialId = this.idD
      //console.log("898989898989898", this.queryParams)
      listInventoryInquiry(this.queryParams).then(response => {
        this.inventoryInquiryList = response.rows;
        //console.log(response)
        console.log(response)
        this.total = response.total;
        this.loading = false;
      });
    },
    //选择产品
    handleMaterialChange(materialId) {
      //console.log("mamaaa", materialId)
      const selectedMaterial = this.inventoryInquiryList.find(item => item.materialId === materialId);
      if (selectedMaterial) {
        this.form.materialName = selectedMaterial.materialName
        this.form.materialCode = selectedMaterial.materialCode;
        this.form.materialId = selectedMaterial.materialId;
        this.form.nventoryQuantity = selectedMaterial.inventoryDifference;
      }
    },
    a(value) {
      this.queryParams.nventoryQuantity = value.replace(/\D/g, '');
    },
    aa(value) {
      this.form.nventoryQuantity = value.replace(/\D/g, '');
    },
    b(value) {
      this.queryParams.safetyStock = value.replace(/\D/g, '');
    },
    bb(value) {
      this.form.safetyStock = value.replace(/\D/g, '');
    },
    c(value) {
      this.queryParams.maximumInventory = value.replace(/\D/g, '');
    },
    cc(value) {
      this.form.maximumInventory = value.replace(/\D/g, '');
    },
    d(value) {
      this.queryParams.minimumStock = value.replace(/\D/g, '');
    },
    dd(value) {
      this.form.minimumStock = value.replace(/\D/g, '');
    },
    // /** 查询库存预警查询列表 */
    // getwarnList() {
    //   this.loading = true;
    //   listTnventoryWarning(this.queryParams).then(response => {
    //     this.tnventoryWarningList = response.rows;
    //     this.total = response.total;
    //     this.loading = false;
    //   });
    // },
    getwarnList() {
      this.loading = true;
      listTnventoryWarning(this.queryParams).then(response => {
        this.tnventoryWarningList = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    //   tableRowClassName({row, rowIndex}) {
    //     if (row.nventoryQuantity > row.maximumInventory) {
    //       return 'success-row';
    //     } else if (row.nventoryQuantity < row.minimumStock) {
    //       return 'warning-row';
    //     }
    //     return '';
    //
    // },
    //   getTooltipContent(row) {
    //     if (row.nventoryQuantity > row.maximumInventory) {
    //       return '库存过剩';
    //     } else if (row.nventoryQuantity < row.minimumStock) {
    //       return '库存过低';
    //     }
    //     return '水位安全';
    //   },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warningId: null,
        materialCode: null,
        materialName: null,
        nventoryQuantity: null,
        safetyStock: null,
        maximumInventory: null,
        minimumStock: null,
        remark: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      listInventoryInquiry().then(res => {
      })
      this.queryParams.pageNum = 1;
      this.aareset()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warningId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存预警查询";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const warningId = row.warningId || this.ids
      getTnventoryWarning(warningId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存预警查询";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warningId != null) {
            updateTnventoryWarning(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.aareset()
            });
          } else {

            addTnventoryWarning(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.aareset()
            });
          }
        }
      });
    },
    aareset() {
      // 查询参数
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        materialCode: null,
        materialName: null,
        nventoryQuantity: null,
        safetyStock: null,
        maximumInventory: null,
        minimumStock: null,
      }
      this.getwarnList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warningIds = row.warningId || this.ids;
      this.$modal.confirm('是否确认删除库存预警查询编号为"' + warningIds + '"的数据项？').then(function () {
        return delTnventoryWarning(warningIds);
      }).then(() => {
        this.aareset()
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wareManagement/tnventoryWarning/export', {
        ...this.queryParams
      }, `tnventoryWarning_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #e642426b;
}
</style>

